home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_345 / getimage / getimage.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  19KB  |  537 lines

  1. /****************************************************************
  2. *                                                               *
  3. *   GetImage - a DeluxePaint to C data converter (gi)           *
  4. *                                                               *
  5. *   Copyright (c) 1986,  Michael J. Farren                      *
  6. *   This program may be freely distributed by any means         *
  7. *   desired, but may not be sold in any form.                   *
  8. *                                                               *
  9. *      Modification History:                                    *
  10. *                                                               *
  11. *  7/86 Dmil -  Changed input routine to use the input          *
  12. *   filename as the output filename with '.c' tacked on         *
  13. *   the end if only one filename is given upon invocation.      *
  14. *                                                               *
  15. *  Example: GetImage brushfile <cr> will create brushfile.c     *
  16. *                                                               *
  17. *   Added code to write an Image structure along with the       *
  18. *   Image data. Also writes   some pertinent info about the     *
  19. *   image in the file header.                                   *
  20. *                                                               *
  21. *  Note: The changes I've made to this program work fine for me,*
  22. *   and I'm using over 80 images processed by this program as   *
  23. *   gadget images in my current project, some of which are over *
  24. *   100x30 med-res pixels in size in 16 colors. Try that with   *
  25. *   ImageMaker. It may or may not work as well for you.         *
  26. *   Caveat Emptor.                                              *
  27. *                                                               *
  28. * Billions of thanks to Michael J. Farren for making this       *
  29. * source available. It's saved my ass.                          *
  30. *                                                               *
  31. *   David Milligan (Dmil), Integrated Systems, Inc.             *
  32. *                                                               *
  33. *  1/90 CAB - Now searches for BODY label instead of assuming   *
  34. *     that it is 12 bytes passed the GRAB label (which it       *
  35. *     never was on any of my brush files), and now it shows     *
  36. *     all colors and marks the ones that are used instead of    *
  37. *     x'ing out the ones that are not used.                     *
  38. *        Another feature I added, is that it does not print     *
  39. *     any bit plane data if it is all 0's, and it sets up the   *
  40. *     PlanePick value in the Image structure accordingly.       *
  41. *     This cuts down on the size of the object files for        *
  42. *     your images.                                              *
  43. *        Also, adds #include <exec/types.h>  and  #include<     *
  44. *     intuition/intuition.h>  so you can compile the output     *
  45. *     C file immediately and just link it with your other       *
  46. *     files that use the Image data.                            *
  47. *                                                               *
  48. *   Charles A. Brand (CAB), Correct Algorithm Builders, Inc.    *
  49. *                                                               *
  50. *   Using Manx Aztec C ver. 3.6a, compile this file as folows:  *
  51. *                                                               *
  52. *     Compile : cc GetImage -s +L -E200                         *
  53. *     Link    : ln GetImage.o -lc32                             *
  54. *                                                               *
  55. *****************************************************************/
  56.  
  57.  
  58. #include <stdio.h>
  59. #undef NULL
  60. #include <exec/types.h>
  61.  
  62. struct BitMapHeader
  63.   {
  64.    UWORD w,h;         /* raster width, height, in pixels */
  65.    WORD  x,y;           /* pixel position for saved image  */
  66.    UBYTE nplanes;         /* number of bit planes in source  */
  67.    UBYTE masking;         /* masking technique         */
  68.    UBYTE compression;      /* compression algorithm      */
  69.    UBYTE pad1;            /* padding to justify next entry   */
  70.    UWORD transColor;         /* transparent "color number"      */
  71.    UBYTE xAsp, yAsp;         /* x:y aspect ratio         */
  72.    WORD  pageW, pageH;      /* source "page" size in pixels    */
  73.    }  bmhd;
  74.  
  75. UBYTE Cmap[96],old_cmap[96];         /*  32 color storage */
  76. APTR *(raster_lines[400][6]);      /* pointers to raster lines in bit planes */
  77.  
  78. LONG colors_in_byte[8], colors_used[32], tran_table[32];
  79. LONG twopowers[7] = { 1, 2, 4, 8, 16, 32, 64 };
  80. BOOL check;
  81. WORD planes_used[5];
  82. UBYTE PP;
  83. int num_planes;
  84.  
  85. main(argc, argv)
  86. int argc;
  87. char *argv[];
  88. {
  89.    WORD bytes_per_line;             /* Number of bytes per raster line */
  90.    FILE *i_file, *o_file;
  91.    UBYTE *buff_pointer, *body_data;          /* storage pointers */
  92.    UBYTE *data_pointer, *buff_pointer_save;
  93.    LONG body_data_size, buff_size;           /* size storage */
  94.    LONG i, j, k, temp;              /* various temporary variables */
  95.    UBYTE tempbyte;
  96.    char *samename;
  97.    char *add_c = ".c";
  98.  
  99.  
  100.    /*  Check arguments, and try to open i/o files  */
  101.  
  102.    if(argc > 3)    {
  103.       printf("\nUsage -> getimage <input file> <output file>\n");
  104.       exit(20);
  105.    }
  106.  
  107.    if((i_file = fopen(argv[1], "r")) == 0 )    {
  108.       printf("\nCould not open input file %s!\n", argv[1]);
  109.       exit(20);
  110.    }
  111.  
  112.    if(argc = 2)   {
  113.    strcpy(samename,argv[1]);
  114.    strcat(samename,add_c);
  115.       if((o_file = fopen(samename, "w")) == 0)  {
  116.             printf("\nCould not open output file %s!\n", samename);
  117.             exit(20);
  118.       }
  119.    }
  120.    else
  121.       if((o_file = fopen(argv[2], "w")) == 0 )       {
  122.          printf("\nCould not open output file %s!\n", argv[2]);
  123.          exit(20);
  124.       }
  125.  
  126.    /* Files are O.K. - skip over first 20 bytes ("FORM", length, "ILBM",
  127.       "BMHD", length) */
  128.  
  129.    printf("\nReading input file...\n");
  130.    if(fseek(i_file, 20L, 0)) read_error();
  131.  
  132.    /* Now, read in the BMHD structure */
  133.  
  134.    chkread(&bmhd, sizeof(struct BitMapHeader), i_file);
  135.  
  136.  
  137.    /* Skip the CMAP label, and read in the color map length, then the color
  138.       map data.  We assume that the color map length accurately reflects
  139.       the number of bit planes, so don't bother to check the header entry
  140.       bmhd.nplanes.  Also, all Amiga color maps will have an even number of
  141.       entries, so don't bother padding the read out.  */
  142.  
  143.    if(fseek(i_file, 4L, 1)) read_error();          /* skip the label */
  144.    chkread(&temp, sizeof(LONG), i_file);             /* read the length */
  145.  
  146.    i = 0;                              /* and read in the map */
  147.    while( i < temp)
  148.       chkread(&Cmap[i++], 1, i_file);
  149.  
  150.    /* Now, check the next header.  If it isn't "GRAB", this isn't a
  151.       brush file, so get out  */
  152.  
  153.    chkread(&temp, sizeof(LONG), i_file);
  154.    if(temp != ('G'<<24 | 'R'<<16 | 'A'<<8 | 'B'))    {
  155.       printf("\nThe input file is not a DeluxePaint brush file!\n");
  156.       exit(20);
  157.    }
  158.  
  159.    /* It's probably a brush file.  Search for the BODY label, then get
  160.       the body length,then read the body data into body_data[] */
  161.  
  162.    check = FALSE;
  163.    while(check != TRUE) {
  164.       chkread(&temp, sizeof(LONG), i_file);
  165.       if(temp == ('B'<<24 | 'O'<<16 | 'D'<<8 | 'Y')) check = TRUE;
  166.    }
  167.    chkread(&body_data_size, sizeof(LONG), i_file);
  168.    body_data = AllocMem(body_data_size, NULL);
  169.    chkread(body_data, body_data_size, i_file);
  170.  
  171.    /* Now, start the good stuff.  First, allocate enough memory to hold
  172.       the entire bitmap for the image. */
  173.  
  174.    bytes_per_line = (bmhd.w & 7 ? bmhd.w+8 : bmhd.w) >> 3;
  175.    if(bytes_per_line & 1) bytes_per_line++;
  176.  
  177.    buff_size = bytes_per_line * bmhd.h * bmhd.nplanes;
  178.    buff_pointer = AllocMem(buff_size, NULL );
  179.    buff_pointer_save = buff_pointer;
  180.    data_pointer = body_data;
  181.  
  182.    /* Next, go through the file line by line, bit plane by bit plane,
  183.       and extract the image data, putting it into bitmap.  As this is
  184.       being done, save pointers to each line in the raster_lines array. */
  185.  
  186.    /* # of lines high */
  187.    for(i=0; i!=bmhd.h; i++)    {
  188.          /* # of planes      */
  189.          for(j=0; j!=bmhd.nplanes; j++)       {
  190.             raster_lines[i][j] = buff_pointer;      /* set the pointer */
  191.             if(bmhd.compression == 1)    {
  192.              /* if compressed, decompress */
  193.                   expand_map(bytes_per_line, buff_pointer, &data_pointer);
  194.             }
  195.             else     {
  196.                for(k=0; k!=bytes_per_line; k++)    {
  197.                      *(buff_pointer+k) = *(data_pointer++);
  198.                }
  199.             }
  200.             buff_pointer += bytes_per_line;
  201.          }
  202.    }
  203.  
  204.    check_planes(bytes_per_line);
  205.  
  206.    FreeMem(body_data, body_data_size);
  207.  
  208.    /* Now, go through the data, determining the different colors used  */
  209.  
  210. calc_color:
  211.  
  212.    printf("\nAnalyzing data...");
  213.    for(i=0; i!=twopowers[bmhd.nplanes]; colors_used[i++]=0);
  214.       
  215.    for(i=0; i!=bmhd.h; i++) {
  216.       for(j=0; j!=bmhd.w; j++) {
  217.          colors_used[get_a_bit(i,j)] = 1;
  218.       }
  219.    }
  220.  
  221.       /*  Show the current color register stuff */
  222.  
  223. start_over:
  224.  
  225.       printf("\nCurrent color register assignments for this picture are:\n");
  226.       for(i=0; i!=twopowers[bmhd.nplanes]; i++)    {
  227.          if(colors_used[i])   {
  228.             printf("%2d -> R:%2d G:%2d B:%2d      ",
  229.                   i, Cmap[i*3]>>4, Cmap[i*3+1]>>4, Cmap[i*3+2]>>4);
  230.          }
  231.          else    {
  232.             printf("%2d -> Not Used            ",i);
  233.          }
  234.          if(i&1) printf("\n");
  235.       }
  236.  
  237.    /* Now, write the data to the output file, color map first, then data,
  238.          plane by plane, line by line */
  239.  
  240.    printf("\nWriting output file...");
  241.  
  242.          /* Output a few statistics */
  243.  
  244.    /********************************************************
  245.     *                                                      *
  246.     *    Start of Dmil's Image info hack                   *
  247.     *                                                      *
  248.     *  I like to have as much info on the Image I'm        *
  249.     *  working with as possible, especially if I'm trying  *
  250.     *  to use PlanePick & PlaneOnOff, so I write out the   *
  251.     *  following data with the Image data & structure.     *
  252.     *                                                      *
  253.     ********************************************************/
  254.  
  255. /* Put #include statements in source file. */
  256.  
  257.    fprintf(o_file, "#include <exec/types.h>\n");
  258.    fprintf(o_file, "#include <intuition/intuition.h>\n");
  259.  
  260.    fprintf(o_file, "\n");
  261.    fprintf(o_file, "/*   Image %s%s  \n",argv[1],"_image");
  262.    fprintf(o_file, "          Width:    %d\n",bmhd.w);
  263.    fprintf(o_file, "         Height:    %d\n",bmhd.h);
  264.    fprintf(o_file, "          Depth:    %d\n",bmhd.nplanes);
  265.    fprintf(o_file, "     TransColor:    %d  */\n\n",bmhd.transColor);
  266.  
  267.          /* Start with the color map */
  268.  
  269.   fprintf(o_file,"/* Color Map */\n\n");
  270.  
  271.    fprintf(o_file, "USHORT map[] = {\n");
  272.  
  273.    for(i=0; i!=twopowers[bmhd.nplanes]; i++) {
  274.       fprintf(o_file, "   ");
  275.       make_word((Cmap[i*3]<<4) + Cmap[i*3+1] + (Cmap[i*3+2]>>4), o_file);;
  276.       if(i!=twopowers[bmhd.nplanes]-1) fprintf(o_file, ",");
  277.       if(colors_used[i]) fprintf(o_file, "/* USED */");
  278.       fprintf(o_file, "\n");
  279.    }
  280.    fprintf(o_file, "};\n\n");
  281.  
  282.    /* Now, do the data */
  283.  
  284.    fprintf(o_file, "/* Image Data */\n\n");
  285.    fprintf(o_file, "UWORD %s[%d] =\n{\n", argv[1],(num_planes *
  286.       bytes_per_line * bmhd.h)/2);
  287.    for(i=0; i!=bmhd.nplanes; i++)    {
  288.       fprintf(o_file, "/* Bit Plane #%d */\n\n", i);
  289.  
  290. /* If the Bit Plane is all 0's, don't print its data. */
  291.       if(planes_used[i]) {
  292.  
  293.          for(j=0; j!=bmhd.h; j++)   {
  294.             fprintf(o_file, "   ");
  295.             buff_pointer = raster_lines[j][i];
  296.             for(k=0; k<bytes_per_line; )   {
  297.                tempbyte = *(buff_pointer+(k++));
  298.                make_word((tempbyte << 8) | *(buff_pointer+(k++)), o_file);
  299.                fprintf(o_file, ",");
  300.             }
  301.             fprintf(o_file, "\n");
  302.          }
  303.          fprintf(o_file, "\n");
  304.       }
  305.       else  /* Print that the bit plane is not used. */
  306.          fprintf(o_file, "/* This bit plane not used. */\n\n");
  307.    }
  308.    fseek(o_file, -3, 1);        /* back up to wipe out the last comma */
  309.    fprintf(o_file, "\n};\n");
  310.  
  311.    /********************************************************
  312.     *                                                      *
  313.     *    Start of Dmil's Image structure hack              *
  314.     *                                                      *
  315.     ********************************************************/
  316.  
  317. /* Now, Sets up PlanePick value according to which bit planes are used. */
  318.  
  319.    fprintf(o_file, "struct Image %s_image =\n{\n",argv[1]);
  320.    fprintf(o_file, "\t0,0,\t\t\t/* LeftEdge, TopEdge  */\n");
  321.    fprintf(o_file, "\t%d,%d,%d,\t\t/* Width, Height, Depth */\n",bmhd.w,bmhd.h,bmhd.nplanes);
  322.    fprintf(o_file, "\t&%s[0],\t\t/* Pointer to Image data */\n",argv[1]);
  323.    fprintf(o_file, "\t%d,0,\t\t\t/* PlanePick, PlaneOnOff */\n",PP);
  324.    fprintf(o_file, "\tNULL,\t\t\t/* NextImage pointer */\n};\n");
  325.  
  326.    /* Close up, clean up, and get out */
  327.  
  328.    fclose(o_file);
  329.    fclose(i_file);
  330.    printf("\n");
  331.    FreeMem(buff_pointer_save, buff_size);
  332. }
  333.  
  334.    /*  read_error - called if a read error occured */
  335.  
  336. read_error()
  337. {
  338.    printf("\nRead error in input file, aborting\n");
  339.    exit(20);
  340. }
  341.  
  342.    /* chkread - reads from input file, checking for errors */
  343.  
  344. chkread(ptr, size, fd)
  345. APTR *ptr;
  346. WORD size;
  347. FILE *fd;
  348. {
  349.    WORD readin;
  350.    readin = fread(ptr, size, 1, fd);
  351.    if(readin != 1) read_error();
  352. }
  353.  
  354. /* expand_map - decompresses data compressed with the byte_run encoding
  355.                 scheme described in the IFF document.  A signed byte, x,
  356.                 is read from the input block.  If the signed value of x
  357.                 is negative, but not -128 ( 0x80 ), the next byte is read
  358.                 and is placed in the output block (-x+1) times.  If the
  359.                 signed value is positive, the next (x+1) bytes are copied
  360.                 directly to the output block.  If the signed value is -128,
  361.                 no operation is performed.
  362. */
  363.  
  364.  
  365. expand_map(length, pointer, data_pointer)
  366. WORD length;
  367. UBYTE *pointer;
  368. UBYTE **data_pointer;
  369. {
  370.  
  371.    WORD minus128 = -128;
  372.    WORD temp;
  373.    BYTE tempbyte;
  374.    UBYTE tempubyte;
  375.  
  376.    while( length > 0 )   {
  377.       tempbyte = *((*data_pointer)++);
  378.       temp = tempbyte;
  379.       if (temp >= 0)   {
  380.             temp += 1;
  381.             length-=temp;
  382.             do   {
  383.                *(pointer++) = *((*data_pointer)++);
  384.             }   while (--temp > 0);
  385.       }
  386.       else
  387.          if (temp != minus128)   {
  388.             temp = (-temp) + 1;
  389.             length -= temp;
  390.             tempubyte = *((*data_pointer)++);
  391.             do   {
  392.                *(pointer++) = tempubyte;
  393.             }   while (--temp > 0);
  394.          }
  395.    }
  396. }
  397.  
  398. /* Determine which bit-planes are not used and set-up PlanePick value */
  399.  
  400. check_planes(bpl)
  401. WORD bpl;   /* byte_per_line */
  402. {
  403. int i,j,k;
  404. UBYTE *pt; /* pointer to body data */
  405.  
  406. /* Clear planes_used[] array */
  407.  
  408.    for(i=0;i != bmhd.nplanes;i++)
  409.       planes_used[i] = 0;
  410.  
  411. /* Now determine if bit planes are all 0's */
  412.  
  413.    for(i=0;i != bmhd.nplanes;i++) {
  414.       for(j=0;j != bmhd.h;j++) {
  415.          pt = raster_lines[j][i];
  416.          for(k=0;k < bpl;k++)
  417.             planes_used[i] |= *(pt++);
  418.       }
  419.    }
  420.  
  421. /* Set-up PlanePick value
  422.    and number of planes actually used */
  423.  
  424.    num_planes = 0;
  425.    PP = 0;   /* Zero PlanePick variable */
  426.    for(i = (bmhd.nplanes-1);i >= 0;i--) {
  427.       PP = (PP << 1);
  428.       if(planes_used[i]) {
  429.          PP += 1;
  430.          num_planes += 1;
  431.       }
  432.    }
  433. }
  434.  
  435. /* get_a_bit - returns the color value of the bit at location x,y in the
  436.                image.
  437. */
  438.  
  439. get_a_bit(y, x)
  440. LONG y, x;
  441. {
  442.    LONG xbyte, xbit,i;
  443.    UBYTE tempbyte, color;
  444.    UBYTE *pointer;
  445.  
  446.    color = 0;                               /* start with no color */
  447.    xbyte =x>>3;                             /* xbyte = the byte location of x */
  448.    xbit = (1<<(x&7));                       /* xbit = the mask for the bit */
  449.    for(i=0; i!=bmhd.nplanes; i++) 
  450.    {                  /* do all the planes */
  451.       pointer = raster_lines[y][i];             /* get the base address */
  452.       tempbyte = *(pointer + xbyte);            /* get the proper byte  */
  453.       if(tempbyte & xbit) color |= (1 << i);    /* OR in the color bit  */
  454.       }
  455.    return color;
  456. }
  457.  
  458.    /* put_a_bit - sets a given bit to a given color */
  459.  
  460. put_a_bit(y, x, color)
  461. LONG y, x, color;
  462. {
  463.    LONG xbyte, xbit, i;
  464.    UBYTE *pointer;
  465.    UBYTE tempbyte;
  466.  
  467.    for(i=0; i!=bmhd.nplanes; i++) 
  468.    {
  469.          xbyte = x>>3;
  470.          xbit = (1 << (x & 7));
  471.          pointer = raster_lines[y][i];
  472.          tempbyte = *(pointer + xbyte);      /* get the appropriate byte */
  473.       tempbyte &= 0xff-xbit;                 /* mask off the proper bit  */
  474.       if(color & twopowers[i]) 
  475.    {                   /* if the color bit is set, */
  476.          tempbyte |= xbit;                   /* set the bit in the byte  */
  477.          }
  478.       *(pointer + xbyte) = tempbyte;         /* save the modified byte   */
  479.       }
  480. }
  481.  
  482. /* get_new_colors - get the users choices for register assignments */
  483.  
  484. get_new_colors()
  485. {
  486.    int i,j;
  487.  
  488.    for(i=0; i!=twopowers[bmhd.nplanes]; tran_table[i++]=-1);    /* reset */
  489.    for(i=1; i!=twopowers[bmhd.nplanes]; i++) 
  490.    {
  491.          if(colors_used[i]) 
  492.       {              /* for each color used in the original */
  493.       getnew:
  494.             printf("\nOld color register %2d (R:%2d G:%2d B:%2d) new number:",
  495.                i, Cmap[i*3]>>4, Cmap[i*3+1]>>4, Cmap[i*3+2]>>4);
  496.             scanf("%d", &j);
  497.             if((j<1) || (j>=twopowers[bmhd.nplanes])) 
  498.          {
  499.             printf("\nRegister number must be greater than zero, and less than %2d.",
  500.                      twopowers[bmhd.nplanes]);
  501.                   printf("\nTry again.");
  502.                   goto getnew;
  503.                }
  504.             tran_table[i] = j;
  505.             }
  506.       }
  507.  
  508. /* Check the translation table for duplicated register assignments. */
  509.  
  510.    for(i=1; i!=twopowers[bmhd.nplanes]-1; i++) 
  511.    {
  512.          for(j=i+1; j!=twopowers[bmhd.nplanes]; j++) 
  513.       {
  514.             if((tran_table[i]==-1) || (tran_table[j]==-1)) continue;
  515.             if(tran_table[i] == tran_table[j]) 
  516.          {
  517.                   printf("\nDuplicate color register assignment - try again.");
  518.                   return -1;
  519.                }
  520.             }
  521.       }
  522.    return 0;
  523. }
  524.  
  525. /* make_word - outputs a string representing a four-digit hex number */
  526.  
  527. make_word(num, fp)
  528. UWORD num;
  529. FILE *fp;
  530. {
  531.    char temp[10];
  532.  
  533.    sprintf(temp, "0000%x\0", num);
  534.    fprintf(fp, "0x%s", &temp[strlen(temp)-4]);
  535. }
  536.  
  537.